package cn.sibu.myroot.activityorientation;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.EditText;

import cn.sibu.myroot.R;

public class ManualActivity extends AppCompatActivity {

    //引用视图元素
    private EditText mEditText;
    private CheckBox mCheckBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载视图资源
        loadView();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //必须调用超类
        super.onConfigurationChanged(newConfig);
        Log.e("Configuration","onConfigurationChanged");
        //如果选中此复选框,则重新加载新的配置下的元素
        if(mCheckBox.isChecked()){
            final Bundle uiState = new Bundle();
            //保存重要的UI状态
            saveState(uiState);
            //重新加载视图资源
            loadView();
            //恢复UI状态
            restoreState(uiState);
        }
    }
    //实现持久化的UI状态的代码

    private  void saveState(Bundle state){
                  state.putBoolean("checkbox",mCheckBox.isChecked());
                  state.putString("text",mEditText.getText().toString());
    }

    private void restoreState(Bundle state){
        mCheckBox.setChecked(state.getBoolean("checkbox"));
        mEditText.setText(state.getString("text"));
    }

    //设置内容视图并获得视图引用

    private void loadView(){
        setContentView(R.layout.activity_manual);
        //我们必须在设置新的布局时重置视图引用
        mCheckBox = (CheckBox) findViewById(R.id.override);
        mEditText = (EditText) findViewById(R.id.edit_text);
    }
}
